package com.jy.oms.framework.asyn.task.executor.filter;

import com.jy.oms.framework.asyn.task.constants.ConfigConstants;
import com.jy.oms.framework.asyn.task.constants.TaskQueueTypeEnum;
import com.jy.oms.framework.asyn.task.executor.ITaskControllerFilter;
import com.jy.oms.framework.asyn.task.executor.ITaskExtractorFilter;
import com.jy.oms.framework.asyn.task.model.TaskGroup;
import com.jy.oms.framework.asyn.task.model.TaskQueryDto;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 任务黑白名单过滤器
 *
 * @author liuquan
 */
@Service
public class TaskSwitchFilter extends AbstractTaskBlackWhitelistFilter
        implements ITaskExtractorFilter, ITaskControllerFilter {

    @Override
    public boolean checkTaskGroupAbleRun(TaskGroup taskGroup) {
        boolean disableAllTask = this.judgeDisableAllTask();
        return !disableAllTask;
    }

    /**
     * 设置黑白名单过滤查询条件
     */
    @Override
    public void fillExtractorParam(TaskGroup taskGroup, TaskQueryDto taskQueryDto) {
        if (TaskQueueTypeEnum.DB != taskGroup.getTaskQueueType()) {
            return;
        }

        //指定任务可运行
        List<String> whitelist = this.getWhitelist();
        if(CollectionUtils.isNotEmpty(whitelist)){
            if(CollectionUtils.isNotEmpty(taskQueryDto.getTaskCodes())){
                taskQueryDto.getTaskCodes().addAll(whitelist);
            }else{
                taskQueryDto.setTaskCodes(whitelist);
            }
        }

        //指定任务不可运行
        List<String> blacklist = this.getBlacklist();
        if(CollectionUtils.isNotEmpty(blacklist)){
            if(CollectionUtils.isNotEmpty(taskQueryDto.getExcludeTaskCodes())){
                taskQueryDto.getExcludeTaskCodes().addAll(blacklist);
            }else{
                taskQueryDto.setExcludeTaskCodes(blacklist);
            }
        }
    }

    @Override
    protected String getBlacklistConfigKey() {
        return ConfigConstants.EXECUTOR_TASK_BLACKLIST;
    }

    @Override
    protected String getWhitelistConfigKey() {
        return ConfigConstants.EXECUTOR_TASK_WHITELIST;
    }

    @Override
    protected String getAlarmTitle() {
        return "任务黑白名单开关";
    }

}
